package com.adong.crowd.service.impl;

import com.adong.crowd.constant.CrowdConstant;
import com.adong.crowd.entity.Admin;
import com.adong.crowd.entity.AdminExample;
import com.adong.crowd.exception.LoginAcctExistsException;
import com.adong.crowd.exception.LoginFailedException;
import com.adong.crowd.mapper.AdminMapper;
import com.adong.crowd.service.api.AdminService;
import com.adong.crowd.util.CrowdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.BatchUpdateException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 添加方法
     *
     * @param admin
     * @return
     */
    @Override
    public int saveAdmin(Admin admin) {


        String userpswd = admin.getUserPswd();
        String userPswd = passwordEncoder.encode(userpswd);
        admin.setUserPswd(userPswd);

        // 添加时间
        Date data = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = format.format(data);
        admin.setCreateTime(createTime);

        // 执行保存
        return adminMapper.insert(admin);


    }

    /**
     * 获取全部的方法
     *
     * @return
     */

    @Override
    public List<Admin> getAll() {
        return adminMapper.selectByExample(new AdminExample());
    }

    /**
     * 管理员登录的验证
     *
     * @param loginAcct
     * @param userPswd
     * @return
     */
    @Override
    public Admin getAdminByLoginAcct(String loginAcct, String userPswd) {
        // 根据登录创建的账号查询Admin对象
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andLoginAcctEqualTo(loginAcct);
        // 调用adminMapper的方法执行查询结果
        List<Admin> admins = adminMapper.selectByExample(adminExample);

        // 判断合法性
        if (admins == null || admins.size() == 0) {
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }
        if (admins.size() > 1) {
            throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
        }
        Admin admin = admins.get(0);
        // 3.如果 Admin 对象为 null 则抛出异常
        if (admin == null) {
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }

        // 查询出数据库中密码
        String userPswdDB = admin.getUserPswd();
        // 将表单中的密码转换成密文
        String userPswdFrom = CrowdUtil.md5(userPswd);

        if (!Objects.equals(userPswdFrom, userPswdDB)) {
            // 7.如果比较结果是不一致则抛出异常
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }
        // 8.如果一致则返回 Admin 对象
        return admin;

    }


    /**
     * 根据条件进行分页查询
     *
     * @param keyword  搜索值
     * @param pageNum  当前页
     * @param pageSize 每页显示的数量
     * @return
     */
    public PageInfo<Admin> getAdminPage(String keyword, Integer pageNum, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 根据关键词查询出记录
        List<Admin> admins = adminMapper.selectAdminListByKeyword(keyword);

        // 为了方便页面使用将 admins 封装为 PageInfo
        PageInfo<Admin> pageInfo = new PageInfo<Admin>(admins);

        return pageInfo;
    }

    /**
     * 根据id删除员工
     *
     * @param adminId
     * @return
     */
    @Override
    public int removeAdmin(Integer adminId) {
        return adminMapper.deleteByPrimaryKey(adminId);
    }

    /**
     * 根据id查询出admin
     *
     * @param id
     * @return
     */
    @Override
    public Admin getAdminById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新Admin
     *
     * @param admin
     */
    @Override
    public void updateAdmin(Admin admin) {
        //有选择的更新,为null不更新
        adminMapper.updateByPrimaryKeySelective(admin);

    }

    /**
     * 根据账号查询Admin
     *
     * @param loginAcct
     * @return
     */
    @Override
    public Admin getAdminByLoginAcct(String loginAcct) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andLoginAcctEqualTo(loginAcct);
        List<Admin> admins = adminMapper.selectByExample(example);
        Admin admin = admins.get(0);
        return admin;
    }
}
